package xyx.springboot.controller;

import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import xyx.springboot.model.User;
import xyx.springboot.service.UserService;

@RestController //返回数据
@Api(tags={"Mybatis测试"},description = "使用jpa查询")
public class MyBatisTestController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "增加",notes = "新增用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "新增用户成功")})
    @PostMapping(value = "/add")
    public JSONObject add(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id,
                          @ApiParam(value = "用户账号",required = true) @RequestParam(value = "username", required = true) String username,
                          @ApiParam(value = "用户密码",required = true) @RequestParam(value = "password", required = true) String password,
                          @ApiParam(value = "用户姓名",required = true) @RequestParam(value = "realname", required = true) String realname,
                          @ApiParam(value = "用户电话",required = true) @RequestParam(value = "phonumber", required = true) String phonumber){
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setRealname(realname);
        user.setPhonumber(phonumber);
        if (userService.selectUser(id) != null){
            jsonObject.put("result","该用户已存在");
        }else{
            userService.addUser(user);
            jsonObject.put("result",user);
        }
        return jsonObject;
    }

    @ApiOperation(value = "删除",notes = "根据id删除用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "删除用户成功")})
    @PostMapping(value = "/delete")
    public JSONObject delete(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id){
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        user = userService.selectUser(id);
        if (user != null){
            userService.deleteUser(id);
            jsonObject.put("result",user);
        }else{
            jsonObject.put("result","该用户不存在");
        }
        return jsonObject;
    }

    @ApiOperation(value = "更新",notes = "更新用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "更新用户成功")})
    @PostMapping(value = "/update")
    public JSONObject update(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id,
                             @ApiParam(value = "用户账号",required = true) @RequestParam(value = "username", required = true) String username,
                             @ApiParam(value = "用户密码",required = true) @RequestParam(value = "password", required = true) String password,
                             @ApiParam(value = "用户姓名",required = true) @RequestParam(value = "realname", required = true) String realname,
                             @ApiParam(value = "用户电话",required = true) @RequestParam(value = "phonumber", required = true) String phonumber){
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setRealname(realname);
        user.setPhonumber(phonumber);
        if (userService.selectUser(id) != null){
            userService.updateUser(user);
            jsonObject.put("result",user);
        }else{
            jsonObject.put("result","该用户不存在");
        }
        return jsonObject;
    }

    @ApiOperation(value = "查询",notes = "根据id查询用户")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询用户成功")})
    @PostMapping(value = "/select")
    public JSONObject select(@ApiParam(value = "用户id",required = true) @RequestParam(value = "id", required = true) int id){
        JSONObject jsonObject = new JSONObject();
        User user = new User();
        user = userService.selectUser(id);
        if (user != null){
            jsonObject.put("result",user);
        }else{
            jsonObject.put("result","该用户不存在");
        }
        return jsonObject;
    }

    @ApiOperation(value = "登录",notes = "用户名：admin，密码：123456，返回值ResponseEntity类型")
    @PostMapping(value = "/login")
    public ResponseEntity Login(@ApiParam(value = "用户名",required = true) @RequestParam String userName,
                                @ApiParam(value = "密码",required = true) @RequestParam  String password){
        if (userName.equals("admin")&&password.equals("123456")){
            return ResponseEntity.ok("OK");
        }else{
            return ResponseEntity.ok("Fail");
        }
    }
}
